﻿#include "client.h"
#include "ui_client.h"
#include <QHostAddress>

Client::Client(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Client)
{
    ui->setupUi(this);

    this->setFixedSize(500,700);
    ui->sIP->setText("127.0.0.1");
    ui->sPort->setText("8765");

    //创建通信套接字
    client = new QTcpSocket(this);

    // 连接接服务器
    client->connectToHost(QHostAddress(ui->sIP->text()), ui->sPort->text().toInt());

    // 接收数据
    connect(client, &QTcpSocket::readyRead, this, [=]()
    {
       QByteArray array = client->readAll();
       ui->record->append(array);
    });
}

Client::~Client()
{
    delete ui;
}

void Client::on_sendBtn_clicked()
{
    client->write(ui->msg->toPlainText().toUtf8());
    ui->record->append("Client Say: " + ui->msg->toPlainText());

    ui->msg->clear();
}
